﻿using System;
using System.Globalization;
using System.Web.UI.WebControls;
using ContractManagementSystem.BusinessLogic;
using ContractManagementSystem.Entity;

namespace ContractManagementSystem.Controls
{
    public partial class ViewAllConsultant : System.Web.UI.UserControl
    {
        static readonly PagedDataSource Paged = new PagedDataSource();
        static int _pagenumber;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                MessageLabel.Text = "";
                ViewConsultantPanel.Visible = true;
                EditConsultantPanel.Visible = false;
                SearchConsultant();
            }
            Submit.OnClientClick = "return ValidateTextBoxAll(" + MessageLabel.Text + ");";
        }

        public void PagedData()
        {
            Paged.PageSize = 15;
            Paged.CurrentPageIndex = _pagenumber;
            Paged.AllowPaging = true;
            if (Paged.IsFirstPage)
            {
                FirstPageButton.Visible = false;
                PreviewPageButton.Visible = false;
                NextPageButton.Visible = true;
                LastPageButton.Visible = true;
            }
            if (Paged.IsLastPage)
            {
                LastPageButton.Visible = false;
                NextPageButton.Visible = false;
                PreviewPageButton.Visible = true;
                FirstPageButton.Visible = true;
            }
            PageLabel.Text = (_pagenumber + 1) + "/" + Paged.PageCount;
            ViewAllConsultantListView.DataSource = Paged;
            ViewAllConsultantListView.DataBind();
        }

        public void SearchConsultant()
        {
            if (!string.IsNullOrEmpty(FullNameTextBoxSearch.Text))
            {
                Paged.DataSource = ConsultantBusiness.SearchFullName(FullNameTextBoxSearch.Text).DefaultView;
                PagedData();
            }
            else
            {
                Paged.DataSource = ConsultantBusiness.FetchAll(ActiveDropDownList.SelectedIndex).DefaultView;
                PagedData();
            }

        }

        protected void FullNameTextBoxTextChanged(object sender, EventArgs e)
        {
            SearchConsultant();
        }

        protected void ActiveDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            SearchConsultant();
        }

        protected void Submit_Click(object sender, EventArgs e)
        {
            var consultantEntity = new ConsultantEntity
                                       {
                                           ConsultantId = new Guid(ConsultantIdTextBox.Text),
                                           FullName = FullNameTextBox.Text,
                                           Birthday = DateTime.Parse(BirthDateTextBox.Text, CultureInfo.CurrentCulture),
                                           Gender = GenderDropDownList.SelectedIndex,
                                           Email = EmailTextBox.Text,
                                           PhoneNumber = PhoneNumberTextBox.Text,
                                           IsActive = bool.Parse(IsActiveDropDownList.SelectedValue)
                                       };
            MessageLabel.Text = ConsultantBusiness.Update(consultantEntity) ? "Update Success !" : "Update False !";
            ViewConsultantPanel.Visible = true;
            EditConsultantPanel.Visible = false;
            SearchConsultant();
            MessageLabel.Text = "";
        }

        protected void DataPager1_OnPreRender(object sender, EventArgs e)
        {
            //SearchConsultant();
        }

        protected void Back_OnClick(object sender, EventArgs e)
        {
            MessageLabel.Text = "";
            ViewConsultantPanel.Visible = true;
            EditConsultantPanel.Visible = false;
            SearchConsultant();
        }

        protected void ViewAllConsultantListView_OnItemDeleting(object sender, EventArgs e)
        {

        }

        protected void ViewAllConsultantListView_OnItemEditing(object sender, EventArgs e)
        {

        }

        protected void EditLinkButton_OnCommand(object sender, CommandEventArgs e)
        {
            ViewConsultantPanel.Visible = false;
            EditConsultantPanel.Visible = true;

            var consultantId = ViewAllConsultantListView.Items[Convert.ToInt32(e.CommandArgument.ToString(), CultureInfo.CurrentCulture)].FindControl("consultantId") as Label;
            if (consultantId != null) ConsultantIdTextBox.Text = consultantId.Text;

            var fullName = ViewAllConsultantListView.Items[Convert.ToInt32(e.CommandArgument.ToString(), CultureInfo.CurrentCulture)].FindControl("FullName") as Label;
            if (fullName != null) { FullNameTextBox.Text = fullName.Text; }

            var birthDate = ViewAllConsultantListView.Items[Convert.ToInt32(e.CommandArgument.ToString(), CultureInfo.CurrentCulture)].FindControl("BirthDate") as Label;
            if (birthDate != null) { BirthDateTextBox.Text = birthDate.Text; }

            var gender = ViewAllConsultantListView.Items[Convert.ToInt32(e.CommandArgument.ToString(), CultureInfo.CurrentCulture)].FindControl("Gender") as Label;
            if (gender != null) GenderDropDownList.SelectedValue = gender.Text;

            var email = ViewAllConsultantListView.Items[Convert.ToInt32(e.CommandArgument.ToString(), CultureInfo.CurrentCulture)].FindControl("Email") as Label;
            if (email != null) EmailTextBox.Text = email.Text;

            var phoneNumber = ViewAllConsultantListView.Items[Convert.ToInt32(e.CommandArgument.ToString(), CultureInfo.CurrentCulture)].FindControl("PhoneNumber") as Label;
            if (phoneNumber != null) PhoneNumberTextBox.Text = phoneNumber.Text;

            var active = ViewAllConsultantListView.Items[Convert.ToInt32(e.CommandArgument.ToString(), CultureInfo.CurrentCulture)].FindControl("IsActive") as Label;
            if (active != null) IsActiveDropDownList.Text = active.Text;
        }

        protected void DeleteButton_OnCommand(object sender, CommandEventArgs e)
        {
            var consultantEntity = new ConsultantEntity { ConsultantId = new Guid(e.CommandArgument.ToString()) };
            ConsultantBusiness.Delete(consultantEntity);
            SearchConsultant();
        }

        protected void LastPageButton_Click(object sender, EventArgs e)
        {
            _pagenumber = Paged.PageCount - 1;
            SearchConsultant();
            LastPageButton.Visible = false;
        }

        protected void FirstPageButton_Click(object sender, EventArgs e)
        {
            _pagenumber = 0;
            SearchConsultant();
            FirstPageButton.Visible = false;
        }

        protected void PreviewPageButton_Click(object sender, EventArgs e)
        {
            if (!Paged.IsFirstPage)
            {
                _pagenumber--;
                SearchConsultant();
                NextPageButton.Visible = true;
                LastPageButton.Visible = true;
            }
        }

        protected void NextPageButton_Click(object sender, EventArgs e)
        {
            if (!Paged.IsLastPage)
            {
                _pagenumber++;
                SearchConsultant();
                PreviewPageButton.Visible = true;
                FirstPageButton.Visible = true;
            }
        }
    }
}